package com.foreknow.dao.impl;

import com.foreknow.bean.Sys_user;
import com.foreknow.dao.Sys_userDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.SQLException;
import java.util.List;

public class Sys_userDaoImpl extends BaseDao implements Sys_userDao {

    //添加信息
    @Override
    public boolean save(Sys_user sys_user) throws SQLException{
        int isRight = jdbcTemplate.update("insert into sys_user(name,sex,age,marry,address,phone) values(?,?,?,?,?,?)",sys_user.getName(),sys_user.getSex(),sys_user.getAge(),sys_user.getMarry(),sys_user.getAddress(),sys_user.getPhone());
        return isRight==1;
    }

    //根据id查询信息
    @Override
    public Sys_user queryById(Integer id) throws SQLException{
        EntityMapping entityMapping = mappingFactory.getMapping(mappingFactory.SYS_USER_MAPPING);
        List<Object> list = jdbcTemplate.query("select * from sys_user where id = ?",entityMapping,id);
        if(list.size()>0){
            return (Sys_user)list.get(0);
        }
        return null;
    }

    //修改
    @Override
    public boolean update(Sys_user sys_user) throws SQLException{
        int isRight = jdbcTemplate.update("update sys_user set name = ?,sex = ?,age = ?,marry = ?,address=?,phone=? where id = ?",sys_user.getName(),sys_user.getSex(),sys_user.getAge(),sys_user.getMarry(),sys_user.getAddress(),sys_user.getPhone(), sys_user.getId());
        return isRight==1;
    }

    //根据id删除
    @Override
    public boolean delete(Integer id) throws SQLException{
        int isRight = jdbcTemplate.update("delete from sys_user where id=?",id);
        return isRight==1;
    }

    //查询所有的信息
    @Override
    public List<Object> getQueryAll() throws SQLException{
        EntityMapping entityMapping = mappingFactory.getMapping(mappingFactory.SYS_USER_MAPPING);
        List<Object> list = jdbcTemplate.query("select * from sys_user",entityMapping);
        return list;
    }
}
